#define relay_pin 7
#define NTC_pin A0
#define knob_pin A1
#define NTC_B 3950// NTC B值
#define NTC_25_R 10000 //NTC 在25℃的阻值
#define NTC_S_R 10000 //NTC串联电阻阻值

#include <math.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <SimpleTimer.h>
static const unsigned char PROGMEM jiaretaiwenkongxitong[288] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0xC0, 0x31, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x04, 0x00, 0xE0, 0x01, 0xC3, 0x00, 0x30, 0x00, 0xC0, 0x30, 0x00, 0x38, 0x00, 0xF7, 0x1F, 0x18, 0x1C, 0xC0, 0xFF, 0x03, 0x83, 0x09, 0x30, 0x00, 0xC0, 0x32, 0x00, 0x1C, 0x00, 0x36, 0x18, 0x18, 0x19, 0xE0, 0x1F, 0x80, 0xF9, 0x1F, 0xFE, 0xFF, 0xF9, 0xFF, 0x01, 0x8E, 0x00, 0x2E, 0x18, 0xFF, 0xFF, 0x01, 0x8E, 0xC0, 0xCD, 0x01, 0xB0, 0x8D, 0xD1, 0xBC, 0x01, 0x87, 0x03, 0xE8, 0x1F, 0x9A, 0x87, 0x81, 0xC3, 0xC1, 0xCE, 0x00, 0xB0, 0x8D, 0xC0, 0xB6, 0x80, 0x03, 0x87, 0x2D, 0x18, 0xD8, 0xFF, 0xC0, 0xFF, 0xE0, 0x67, 0x07, 0x90, 0x8D, 0xC0, 0x9F, 0xC0, 0xFF, 0x8F, 0x2F, 0x18, 0x18, 0xE7, 0xC0, 0x3F, 0xE0, 0x33, 0x0E, 0x90, 0x8D, 0xF8, 0xB8, 0xC4, 0xFF, 0x0C, 0xF5, 0x1F, 0xF8, 0xC3, 0x40, 0xCE, 0x80, 0xF9, 0x1F, 0x90, 0x8D, 0xF8, 0xF8, 0x85, 0x00, 0x08, 0x36, 0x18, 0xFC, 0xC1, 0x80, 0xC7, 0xC1, 0x71, 0x1B, 0x98, 0x8D, 0xC0, 0xEC, 0x07, 0xFF, 0x07, 0xFE, 0x3F, 0xDF, 0xFF, 0xE0, 0xFF, 0xE3, 0x6F, 0x03, 0x98, 0x8D, 0xF0, 0x66, 0x07, 0x03, 0x03, 0xDE, 0x36, 0x1E, 0x0C, 0xE0, 0x1F, 0xE3, 0x61, 0x03, 0x98, 0x8D, 0xC0, 0x03, 0x07, 0x03, 0xC3, 0xDB, 0x36, 0x18, 0x0C, 0x80, 0x7B, 0x02, 0x6C, 0x03, 0x8C, 0x8D, 0x20, 0x99, 0x01, 0x03, 0x03, 0xDB, 0x36, 0x18, 0x0C, 0x80, 0xDB, 0xC1, 0x6F, 0x13, 0xFC, 0xFC, 0x31, 0x33, 0x03, 0x03, 0x03, 0xDB, 0x36, 0x18, 0x8C, 0xC0, 0x98, 0xE3, 0x61, 0x13, 0xE6, 0x8C, 0x31, 0x33, 0x03, 0x03, 0x03, 0xDB, 0x76, 0xD8, 0xFF, 0x71, 0x18, 0x67, 0x30, 0x1B, 0x43, 0x8C, 0x39, 0x33, 0x02, 0xFF, 0x03, 0xFF, 0xFF, 0x5E, 0x00, 0x38, 0x1F, 0x07, 0x38, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x04, 0x00, 0x1C, 0x00, 0x08, 0x0C, 0x02, 0x0E, 0x00};
static const unsigned char PROGMEM mubiaowendu[128] = {0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80, 0x00, 0xFC, 0x1F, 0x88, 0x3F, 0xC4, 0x1F, 0x00, 0x01, 0x04, 0x10, 0x08, 0x00, 0x48, 0x10, 0xFC, 0x7F, 0x04, 0x10, 0x08, 0x00, 0x48, 0x10, 0x44, 0x04, 0x04, 0x10, 0x3F, 0x00, 0xC1, 0x1F, 0x44, 0x04, 0xFC, 0x1F, 0xC8, 0x7F, 0x42, 0x10, 0xFC, 0x3F, 0x04, 0x10, 0x0C, 0x04, 0x42, 0x10, 0x44, 0x04, 0x04, 0x10, 0x1C, 0x04, 0xC8, 0x1F, 0x44, 0x04, 0x04, 0x10, 0xAA, 0x14, 0x08, 0x00, 0xC4, 0x07, 0xFC, 0x1F, 0xAA, 0x24, 0xE4, 0x3F, 0x04, 0x00, 0x04, 0x10, 0x49, 0x24, 0x27, 0x25, 0xF4, 0x0F, 0x04, 0x10, 0x48, 0x44, 0x24, 0x25, 0x24, 0x08, 0x04, 0x10, 0x28, 0x44, 0x24, 0x25, 0x42, 0x04, 0x04, 0x10, 0x08, 0x04, 0x24, 0x25, 0x82, 0x03, 0xFC, 0x1F, 0x08, 0x05, 0xF4, 0x7F, 0x61, 0x0C, 0x04, 0x10, 0x08, 0x02, 0x00, 0x00, 0x1C, 0x70};
static const unsigned char PROGMEM dangqianwendu[128] = {0x80, 0x00, 0x08, 0x08, 0x00, 0x00, 0x80, 0x00, 0x84, 0x10, 0x10, 0x08, 0xC4, 0x1F, 0x00, 0x01, 0x88, 0x10, 0x10, 0x04, 0x48, 0x10, 0xFC, 0x7F, 0x90, 0x08, 0xFF, 0x7F, 0x48, 0x10, 0x44, 0x04, 0x90, 0x04, 0x00, 0x00, 0xC1, 0x1F, 0x44, 0x04, 0x80, 0x00, 0x7C, 0x10, 0x42, 0x10, 0xFC, 0x3F, 0xFE, 0x1F, 0x44, 0x12, 0x42, 0x10, 0x44, 0x04, 0x00, 0x10, 0x44, 0x12, 0xC8, 0x1F, 0x44, 0x04, 0x00, 0x10, 0x7C, 0x12, 0x08, 0x00, 0xC4, 0x07, 0x00, 0x10, 0x44, 0x12, 0xE4, 0x3F, 0x04, 0x00, 0xFC, 0x1F, 0x44, 0x12, 0x27, 0x25, 0xF4, 0x0F, 0x00, 0x10, 0x7C, 0x12, 0x24, 0x25, 0x24, 0x08, 0x00, 0x10, 0x44, 0x10, 0x24, 0x25, 0x42, 0x04, 0x00, 0x10, 0x44, 0x10, 0x24, 0x25, 0x82, 0x03, 0xFE, 0x1F, 0x54, 0x14, 0xF4, 0x7F, 0x61, 0x0C, 0x00, 0x10, 0x24, 0x08, 0x00, 0x00, 0x1C, 0x70};
static const unsigned char PROGMEM sheshidu[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0xFE, 0x32, 0xE3, 0xB3, 0xC1, 0xF2, 0x81, 0xDE, 0x01, 0xC0, 0x01, 0xC0, 0x01, 0xC0, 0x01, 0x80, 0x81, 0x80, 0xC3, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00};

volatile int targetTemp, currentTemp;
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
SimpleTimer timer;

void Simple_timer_1() {
  u8g2.firstPage();
  do
  {
    page1();
  } while (u8g2.nextPage());
  targetTemp = (map(analogRead(NTC_pin), 1023, 0, 50, 260));
  Serial.println(targetTemp);
  double val = analogRead(knob_pin);
  double fenya = (val / 1023) * 5;
  double r = fenya / (5 - fenya) * NTC_S_R;
  currentTemp = 1 / (  log(r / NTC_25_R) / NTC_B + 1 / (25 + 273.15)) - 273.15;
  Serial.println(currentTemp);
  if (currentTemp > targetTemp * 1.05) {
    digitalWrite(relay_pin, HIGH);
  } else if (currentTemp < targetTemp * 0.95) {
    digitalWrite(relay_pin, LOW);
  }
}

void page1() {
  u8g2.drawXBMP(0, 2, 126, 18, jiaretaiwenkongxitong);
  u8g2.drawXBMP(0, 22, 64, 16, mubiaowendu);
  u8g2.drawXBMP(0, 42, 64, 16, dangqianwendu);
  u8g2.drawXBMP(110, 22, 16, 16, sheshidu);
  u8g2.drawXBMP(110, 42, 16, 16, sheshidu);
  u8g2.setFont(u8g2_font_timR18_tf);
  u8g2.setFontPosTop();
  u8g2.setCursor(68, 22);
  u8g2.print(targetTemp);
  u8g2.setCursor(68, 42);
  u8g2.print(currentTemp);
}

void setup() {
  targetTemp = 0;
  u8g2.setI2CAddress(0x3C * 2);
  u8g2.begin();
  currentTemp = 0;
  Serial.begin(9600);
  pinMode(relay_pin, OUTPUT);
  timer.setInterval(1000L, Simple_timer_1);
  u8g2.enableUTF8Print();
}

void loop() {
  timer.run();
}
